package com.xhwl.centiotdata.handler.pedestrian.impl;

import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationFactory;
import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.keytoplocal.AccessInfoKeyTopLocalDTO;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IPedestrianCentService;
import com.xhwl.centiotdata.suppliersystem.keytopLocal.PedestrianKeyTopLocalHandler;
import com.xhwl.common.dto.auth.PedestrianAuthDTO;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.dto.cent.pedes.OpenDoorRecordDTO;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.enums.pedestrian.DoorActionEnum;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.auth.AuthAccountMapping;
import com.xhwl.common.utils.ChkUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author msy
 * @description  对接科拓人行2.0-本地
 */
@Slf4j
@Service
public class PedestrianIntegrationKeyTopLocalHandler extends PedestrianIntegrationHandler {

    @Autowired
    private PedestrianKeyTopLocalHandler pedestrianKeyTopLocalHandler;

    @Autowired
    private IPedestrianCentService pedestrianCentService;


    @Override
    public void afterPropertiesSet() throws Exception {
        PedestrianIntegrationFactory.register(ProductEnum.KEYTOP_LOCAL.getId(), this);
    }


    public void pushRecord(AccessInfoKeyTopLocalDTO record, PedestrianIntegrationConfigDTO config) {
        if(record != null) {
            // 转换数据
            List<OpenDoorRecordDTO> openDoorRecordDTOList = pedestrianKeyTopLocalHandler.handleRecord(record, config);
            // 存储业务中台
            pedestrianCentService.saveBatch(openDoorRecordDTOList);
        }
    }

    @Override
    public ResultJson openDoor(PedestrianIntegrationConfigDTO pedestrianIntegrationConfigDTO, DeviceDTO deviceDTO, DoorActionEnum doorAction) {
        return pedestrianKeyTopLocalHandler.openDoor(pedestrianIntegrationConfigDTO, deviceDTO, doorAction);
    }

    @Override
    public ResultJson createAuth(PedestrianIntegrationConfigDTO configDTO, PedestrianAuthDTO pedestrianAuthDTO) {
        // 保存人员
        AuthAccountMapping accountMapping = pedestrianKeyTopLocalHandler.createUser(configDTO, pedestrianAuthDTO);
        String openId = accountMapping.getOpenId();
        pedestrianAuthDTO.setUserOpenId(openId);

        String faceUrl = pedestrianAuthDTO.getFaceUrl();
        if(ChkUtil.isNotNull(faceUrl)) {
            // 保存人员认证信息（添加人脸）
            pedestrianKeyTopLocalHandler.addFace(openId, faceUrl, configDTO);
            log.info("保存人脸成功！");
        }
        // 新增人员门禁权限
        pedestrianKeyTopLocalHandler.saveAuth(configDTO, pedestrianAuthDTO);
        log.info("授权成功！");
        return ResultJson.success();
    }


    @Override
    public ResultJson updateAuth(PedestrianIntegrationConfigDTO configDTO, PedestrianAuthDTO pedestrianAuthDTO) {
        AuthAccountMapping accountMapping = pedestrianKeyTopLocalHandler.createUser(configDTO, pedestrianAuthDTO);
        String openId = accountMapping.getOpenId();
        pedestrianAuthDTO.setUserOpenId(openId);

        String faceUrl = pedestrianAuthDTO.getFaceUrl();
        if(ChkUtil.isNotNull(faceUrl)) {
            // 保存人员认证信息（添加人脸）
            pedestrianKeyTopLocalHandler.addFace(openId, faceUrl, configDTO);
            log.info("修改人脸成功！");
        }

        pedestrianKeyTopLocalHandler.saveAuth(configDTO, pedestrianAuthDTO);
        log.info("修改授权成功！");
        return ResultJson.success();
    }

    @Override
    public ResultJson deleteAuth(PedestrianIntegrationConfigDTO configDTO, PedestrianAuthDTO pedestrianAuthDTO) {
        pedestrianKeyTopLocalHandler.deleteAuth(configDTO, pedestrianAuthDTO);
        log.info("删除授权成功！");
        return ResultJson.success();
    }

}
